package com.cityp.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cityp.bean.Question;
import com.cityp.bean.ResultBean;
import com.cityp.service.impl.questionServiceImpl;
import com.cityp.service.questionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "问题模块接口")
@RestController
@RequestMapping("/Question")
public class questionController {


    @Autowired
    private questionService questionService;
    @Autowired
    private questionServiceImpl questionServiceImpl;

    @ApiOperation(value = "分页查找题库，需要两个参数")
    @RequestMapping(value = "/quesView",method = RequestMethod.GET)
    @ResponseBody
    public ResultBean viewAll(@RequestParam int sum,@RequestParam int num){
        List<Question> res=questionService.get(sum,num);
        if (res==null) return ResultBean.err("出现错误，请重试");
        else return ResultBean.success( "查询成功",res);
    }

    @ApiOperation(value = "仅插入一条题目")
    @PostMapping(value = "/insertOne")
    public ResultBean insertOne(@RequestBody @ApiParam(value = "参数为一个Question实体") Question question){
        question.setId(null);
        if(questionService.insertOne(question)>=1){
            return ResultBean.success("插入成功");
        }
        else{
            return ResultBean.err("插入失败");
        }
    }

    @ApiOperation(value = "删除一个题目")
    @GetMapping(value = "/deleteOne")
    public ResultBean deleteOne(@RequestParam @ApiParam(value = "参数为题目的id") int qid){
        if(questionServiceImpl.deleteOne(qid)>=1){
            return ResultBean.success("删除成功");
        }
        else{
            return ResultBean.err("删除失败，请确认id是否存在");
        }
    }

    @ApiOperation(value = "批量删除题目")
    @PostMapping(value = "/deleteBatch")
    public ResultBean deleteBatch(@RequestParam @ApiParam(value = "参数为题目的id,类型为集合") List<Integer> qid){
        if(questionServiceImpl.delete(qid)>=1){
            return ResultBean.success("删除成功");
        }
        else{
            return ResultBean.err("删除失败，请确认id是否存在");
        }
    }

    @ApiOperation(value = "修改题目内容（非选项）")
    @GetMapping(value = "/updateQuestion")
    public ResultBean updateQuestion(@RequestParam @ApiParam(value = "参数为新题目的所有内容,仅是题目内容，非答案") String question,@RequestParam @ApiParam(value = "题目的id非qb_id")int qid){
        if(question==null||question.equals(" ")){
            return ResultBean.err("非法输入");
        }
        UpdateWrapper<Question> updateWrapper = new UpdateWrapper<>();
        Question question1 = new Question();
        question1.setQbQuestion(question);
        updateWrapper.eq("id",qid);
        if (questionServiceImpl.update(question1,updateWrapper)) {
            return ResultBean.success("更新成功");
        }
        else{
            return ResultBean.err("更新失败");
        }
    }

    @ApiOperation(value = "按类型选择题目")
    @GetMapping(value = "/selectByType")
    public ResultBean selectByType(@RequestParam @ApiParam(value = "参数为题目的类型，如1、2、3")int tid,@RequestParam @ApiParam(value = "分页的偏移量")int offset,@RequestParam @ApiParam("每一页的行数")int rows){
       return ResultBean.success("返回题目成功",questionServiceImpl.selectByType(tid, offset, rows));
    }

    @ApiOperation(value = "查找所有题目")
    @GetMapping(value = "/findAll")
    public ResultBean findAll(){
        return ResultBean.success("查找成功",questionServiceImpl.findAll());
    }
}
